package com.github.prontera.service;

import java.time.OffsetDateTime;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.mapper.JsonMapper;
import com.github.prontera.config.ConfigUtil;
import com.github.prontera.domain.AppSms;
import com.github.prontera.domain.enums.AppSmsSendStatus;
import com.github.prontera.domain.enums.AppSmsTemplateEnum;
import com.github.prontera.persistence.CrudMapper;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 短信
 */
@Service
public class AppSmsService extends CrudServiceImpl<AppSms>{

	@Autowired
	private ConfigUtil configUtil;
	
	
	public AppSmsService(CrudMapper<AppSms> mapper) {
		super(mapper);
	}

	/**
	 * 这里发送的是没有token的这类用户短信
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public AppSms sendToUserID(Map<String, Object> arguments,Long userId,String mobile,AppSmsTemplateEnum template,String code,Date activeTime) {
		AppSms appVerifCode = new AppSms();
		//appVerifCode.setMessage(AppSmsTemplate.getMsg(template.getDesc(), arguments));
		AppSmsSendStatus sendStatus=AppSmsSendStatus.FAIL;
		try{
			TaobaoClient client = new DefaultTaobaoClient(configUtil.getSmsUrl(), configUtil.getSmsAppkey(), configUtil.getSmsSecret());
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend("123456");
			req.setSmsType("normal");
			req.setSmsFreeSignName("易路领鲜");
			req.setSmsParamString(JsonMapper.nonDefaultMapper().toJson(arguments));
			req.setRecNum(mobile);
			req.setSmsTemplateCode(template.getDesc());
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			System.out.println(mobile+"  :   "+ JsonMapper.nonDefaultMapper().toJson(arguments)  +"发送结果为 ======      "+ rsp.getBody());
			if(rsp.isSuccess()) {
				sendStatus=AppSmsSendStatus.SUCCESS;
			}else{
				Shift.fatal(StatusCode.SMS_SEND_ERROR);
			}
		}catch(Exception e){
			e.printStackTrace();
			Shift.fatal(StatusCode.SMS_SEND_ERROR);
		}
		appVerifCode.setTemplate(template);
		appVerifCode.setCode(code);
		appVerifCode.setActiveTime(OffsetDateTime.ofInstant(activeTime.toInstant(), TimeZone.getDefault().toZoneId()));
		appVerifCode.setUserId(userId);
		appVerifCode.setSendStatus(sendStatus);
		appVerifCode.setPhone(mobile);
		if(mapper.insert(appVerifCode)<=0){
			Shift.fatal(StatusCode.DUPLICATE_KEY);
		};
		return appVerifCode;
	}

	/**
	 * 这里发送的是没有token的这类用户短信
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public AppSms sendToUID(Map<String, Object> arguments,String mobile,AppSmsTemplateEnum template, String code,Date activeTime) {
		AppSms appVerifCode = new AppSms();
		//appVerifCode.setMessage(AppSmsTemplate.getMsg(template.getDesc(), arguments));
		AppSmsSendStatus sendStatus=AppSmsSendStatus.FAIL;
		try{
			TaobaoClient client = new DefaultTaobaoClient(configUtil.getSmsUrl(), configUtil.getSmsAppkey(), configUtil.getSmsSecret());
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend("123456");
			req.setSmsType("normal");
			req.setSmsFreeSignName("易路领鲜");
			req.setSmsParamString(JsonMapper.nonDefaultMapper().toJson(arguments));
			req.setRecNum(mobile);
			req.setSmsTemplateCode(template.getDesc());
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			System.out.println(mobile+"  :   "+ code  +"发送结果为 ======      "+ rsp.getBody());
			if(rsp.isSuccess()) {
				sendStatus=AppSmsSendStatus.SUCCESS;
			}else{
				Shift.fatal(StatusCode.SMS_SEND_ERROR);
			}
		}catch(Exception e){
			e.printStackTrace();
			Shift.fatal(StatusCode.SMS_SEND_ERROR);
		}
		appVerifCode.setTemplate(template);
		appVerifCode.setCode(code);
		if(activeTime!=null){
			appVerifCode.setActiveTime(OffsetDateTime.ofInstant(activeTime.toInstant(), TimeZone.getDefault().toZoneId()));
		}
		appVerifCode.setSendStatus(sendStatus);
		appVerifCode.setPhone(mobile);
		if(mapper.insert(appVerifCode)<=0){
			Shift.fatal(StatusCode.DUPLICATE_KEY);
		};
		return appVerifCode;
	}

}